package rayTracer;

import javax.vecmath.Matrix4d;
import javax.vecmath.Point3d;
import javax.vecmath.Quat4d;
import javax.vecmath.Vector3d;

import static java.lang.Math.*;

public class Transformation {
	public Matrix4d transformMatrix;
	Matrix4d m;
	
	public Transformation(){
		transformMatrix = new Matrix4d();
		m = new Matrix4d();
		transformMatrix.setIdentity();
		m.setIdentity();
	}
	
	//Angulos sexagesimales como parametros de entrada
	public void rotX( double angle ){
		angle = angle % 360;
		m.setIdentity();
		m.rotX(toRadians(angle));
		transformMatrix.mul(m, transformMatrix);
	}
	
	public void rotY( double angle ){
		angle = angle % 360;
		m.setIdentity();
		m.rotY(toRadians(angle));
		transformMatrix.mul(m, transformMatrix);		
	}
	
	public void rotZ( double angle ){
		angle = angle % 360;
		m.setIdentity();
		m.rotZ(toRadians(angle));
		transformMatrix.mul(m, transformMatrix);
	}
	
	
	public void scaleX( double factor ){
		transformMatrix.m00 = factor;
	}
	
	public void scaleY( double factor ){
		transformMatrix.m11 = factor;
	}
	
	public void scaleZ( double factor ){
		transformMatrix.m22 = factor;
	}
	
	public void scaleU( double factor){
		transformMatrix.setScale(factor);		
	}
	
	public void translate( double[] transValues ){
		Vector3d transVector = new Vector3d(transValues);
		transformMatrix.setTranslation(transVector);
		
	}
	
	public void rotTransScale(Quat4d q, Vector3d t, double s){
		transformMatrix.set(q, t, s);
	}
	//Realiza todas las transformaciones que estan guardadas en la matriz
	public void transformPoint( Point3d point ){
		transformMatrix.transform(point);
	}
	
	//Solo realiza la traslacion que esta guardada en la matriz
	public void translatePoint( Point3d point){
		Matrix4d translationMatrix = new Matrix4d();
		translationMatrix.setIdentity();
		translationMatrix.m03 = translationMatrix.m03;
		translationMatrix.m13 = translationMatrix.m13;
		translationMatrix.m23 = translationMatrix.m23;
		translationMatrix.transform(point);
	}
	//Realiza todas las transformaciones guardadas MENOS el escalado
	public void translateRotate(Point3d point){
		Matrix4d translateRotateMatrix = new Matrix4d(transformMatrix);
		translateRotateMatrix.m00 = 1;
		translateRotateMatrix.m11 = 1;
		translateRotateMatrix.m22 = 1;
		translateRotateMatrix.transform(point);
	}
	
	public String toString(){
		StringBuffer str = new StringBuffer("");
		str.append("TRANSFORM MATRIX\n");
		str.append(transformMatrix.toString());
		return str.toString();
	}
	
    
    
}
